package com.zoro.dyformservice.utils;

import java.util.Collection;
import java.util.List;

/**
 * @author zhaoxingwu
 */
public class StringUtils {

    public static boolean isBlank(CharSequence str) {
        return org.apache.commons.lang3.StringUtils.isBlank(str);
    }

    public static boolean isNotBlank(CharSequence str) {
        return !isBlank(str);
    }

    public static boolean isAnyBlank(final CharSequence... css) {
        return org.apache.commons.lang3.StringUtils.isAnyBlank(css);
    }

    public static String join(Collection<?> objects, String sp) {
        return org.apache.commons.lang3.StringUtils.join(objects, sp);
    }

    public static String join(Object... objects) {
        return join(List.of(objects), ",");
    }



    /**
     * 去掉两端的斜线
     * @param source string
     * @return 处理后的
     */
    public static String trimSlash(String source){
        return trimChar(source, '/');
    }

    /**
     * 去掉两端的字符
     * @param source string
     * @param c 要去掉的字符
     * @return 处理后的
     */
    public static String trimChar(String source, char c) {
        if (isBlank(source)) {
            return source;
        }
        String s = String.valueOf(c);

        int length = s.length();
        String result = source;
        while (result.startsWith(s)) {
            result = result.substring(length);
        }
        while (result.endsWith(s)) {
            result = result.substring(0, result.length() - length);
        }

        return result;
    }


    public static boolean equal(CharSequence a, CharSequence b) {
        return org.apache.commons.lang3.StringUtils.equals(a, b);
    }

    /**
     * 获取url参数中的值
     *
     * @param queryString url参数
     * @param key         参数名
     * @return value
     */
    public static String getParamFromQueryString(String queryString, String key) {
        if (StringUtils.isBlank(queryString) || StringUtils.isBlank(key)) {
            return null;
        }

        String[] parameters = queryString.split("&");
        for (String p : parameters) {
            String[] pair = p.split("=");
            if (pair.length <= 1) {
                return null;
            }
            if (equal(key, pair[0])) {
                return pair[1];
            }
        }
        return null;
    }

    public static boolean contains(CharSequence a, CharSequence searchSeq) {
        return org.apache.commons.lang3.StringUtils.contains(a, searchSeq);
    }
}
